<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
       <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
       </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.hid.id, isView: true })">
          {{ record.hid.fbillno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhPdList">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { Popconfirm } from 'ant-design-vue';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    whPdStockDelete,
    whPdStockListData,
    synPdStock,
    subListData,
  } from '/@/api/wms/wh/pd/whPdStock';
  import { createStockBill } from '/@/api/wms/wh/pd/whPdStockBill';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import InputForm from './form.vue';
  import { companyTreeData } from '/@/api/sys/company';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('wh.pd.whPdStock');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('盘点单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'companyCode',
        fieldLabel: 'companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('单据日期'),
        field: 'hid.fdate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('单据号'),
        field: 'hid.fbillno',
        component: 'Input',
      },
      {
        label: t('单据状态'),
        field: 'hid.djStatus',
        component: 'Select',
        componentProps: {
          dictType: 'wms_pd_status',
          allowClear: true,
        },
      },
      {
        label: t('物料编码'),
        field: 'basInv.viewCode',
        component: 'Input',
      },
      {
        label: t('物料名称'),
        field: 'fitemname',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'hid.fexplanation',
        component: 'Input',
      },
      // 仓库
      {
        label: t('仓库'),
        field: 'fstockname',
        component: 'Input',
      },
      {
        label: t('是否推送'),
        field: 'hid.bsend',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('推送时间'),
        field: 'hid.senddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
        },
      },
      {
        label: t('制单人'),
        field: 'hid.fbiller',
        component: 'Input',
      },
      // 物料
    ],
    fieldMapToTime: [
      ['hid.senddate', ['hid.senddate_gte', 'hid.senddate_lte']],
      ['hid.fdate', ['hid.fdate_gte', 'hid.fdate_lte']],
    ],
  };
  const tableColumns: BasicColumn[] = [
    //     {
    //   title: t('公司编码'),
    //   dataIndex: 'hid.companyCode',
    //   key: 'hid.company_code',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('公司'),
      dataIndex: 'hid.companyName',
      key: 'hid.company_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单据号'),
      dataIndex: 'hid.fbillno',
      key: 'hid.fbillno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据日期'),
      dataIndex: 'hid.fdate',
      key: 'hid.fdate',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单据状态'),
      dataIndex: 'hid.djStatus',
      key: 'hid.dj_status',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'wms_pd_status',
    },
    {
      title: t('账存数'),
      dataIndex: 'fplanqty',
      width: 100,
      align: 'right',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 16,
      },
      editRule: true,
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('实盘数'),
      dataIndex: 'frealqty',
      width: 100,
      align: 'right',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 16,
      },
      editRule: false,
      showSummary: true,
      showTotalSummary: true,
    },
    // {
    //   title: t('总数'),
    //   dataIndex: 'hid.iqty',
    //   key: 'hid.iqty',
    //   sorter: true,
    //   width: 100,
    //   align: 'right',
    //   showSummary: true,
    // },
    // {
    //   title: t('实盘总数'),
    //   dataIndex: 'hid.sumqty',
    //   key: 'hid.sumqty',
    //   sorter: true,
    //   width: 100,
    //   align: 'right',
    //   showSummary: true,
    // },
    {
      title: t('物料编码'),
      dataIndex: 'basInv.viewCode',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: true,
    },
    {
      title: t('物料内码'),
      dataIndex: 'fitemid',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: true,
    },
    {
      title: t('物料名称'),
      dataIndex: 'fitemname',
      width: 300,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },
    {
      title: t('是否推送'),
      dataIndex: 'hid.bsend',
      key: 'hid.bsend',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'sys_yes_no',
    },
    {
      title: t('推送时间'),
      dataIndex: 'hid.senddate',
      key: 'hid.senddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('盘点方式'),
      dataIndex: 'hid.pdType',
      key: 'hid.pdType',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'wms_pd_type',
    },
    // {
    //   title: t('仓库'),
    //   dataIndex: 'fstockname',
    //   key: 'fstockname',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    // },
    // {
    //   title: t('货位'),
    //   dataIndex: 'hid.poscode',
    //   key: 'hid.poscode',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    // },

    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sys_search_status',
    // },
    {
      title: t('修改时间'),
      dataIndex: 'hid.updateDate',
      key: 'hid.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'hid.remarks',
      key: 'hid.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'hid.fbiller',
      key: 'hid.fbiller',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'hid.fexplanation',
      key: 'hid.fexplanation',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('辅助属性'),
      dataIndex: 'f109',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },
    {
      title: t('仓库内码'),
      dataIndex: 'fstockid',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: true,
    },
    {
      title: t('仓库编码'),
      dataIndex: 'fstockno',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: true,
    },
    {
      title: t('仓库名称'),
      dataIndex: 'fstockname',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },

    {
      title: t('行备注'),
      dataIndex: 'fremarks',
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑盘点单'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'wh:pd:whPdStock:edit',
        ifShow: () => {
          return record.sourceFlag !== '0';
        },
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除盘点单'),
        popConfirm: {
          title: t('是否确认删除盘点单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wh:pd:whPdStock:edit',
        ifShow: () => {
          return record.sourceFlag !== '0';
        },
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRowKeys, getSelectRows, getForm, setProps }] = useTable({
    api: subListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    showCustomSummary: true,
    showTotalCustomSummary: true,
    rowKey: 'fbillno',
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable, isView?: boolean) {
    openDrawer(true, record, isView);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await whPdStockDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  // 同步
  async function handleSyn() {
    const res = await synPdStock({});
    showMessage(res.message);
    handleSuccess({});
  }

  async function handleGenerateTask() {
    let arr = await getSelectRowKeys();
    if (getSelectRowKeys().length != 1) {
      showMessage(t('请选择一行数据'));
      return;
    }
    // openModal(true, {});
    const selIds = arr.join(',');
    const params = { sourceNo: selIds };
    const res = await createStockBill(params);
    console.log(res, 'res');
    showMessage(res.message);
    handleSuccess({});
  }

  async function handlets() {
    if (getSelectRowKeys().length) {
      let selIds = await getSelectRowKeys().join(',');
      const res = await pushK3PdData({ selIds });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择数据');
    }
  }

  async function handleCancel() {
    if (getSelectRowKeys().length == 1) {
      const res = await calBackk3PdData({ id: getSelectRowKeys()[0] });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择一条数据');
    }
  }
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/pd/whPdStock/exportSubData',
      params: getForm().getFieldsValue(),
    });
  }

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
  }

  onMounted(async () => {
    setProps();
    handleCompany();
  });
</script>
